<?php
/**
 * Created by PhpStorm.
 * User: lyh
 * Date: 2017/5/8
 * Time: 16:09
 */
namespace Admin\Controller;

class AuditController extends CommonController{

    //引入审核医生页面
    public function checked(){
        $this->display();
    }
    //提供审核医生的数据接口
    public function getDoctorList(){
        //获取未审核的医生列表 包括医生要加入的团队编号
        $doctors = M('Doctor')->field('id,name,id_no,phone,start_time')->where(['status'=>0])->select();
        //构建数据
        $json_string = [];
        foreach ($doctors as $item){
            $json_string['data'][] = [
                $item['id'],
                $item['name'],
                $item['id_no'],
                $item['phone'],
                date('Y-m-d H:i:s',$item['start_time']),
            ];
        }
        $this->ajaxReturn($json_string);exit;
    }

    /**
     * 审核医生详情页
     * @param $id
     */
    public function getDoctorDetail($id){
        //获取当前审核的医生
        $flag=$_GET['flag'];
        $doctor = M('Doctor')->field('hd_doctor.id,hd_doctor.name,id_no,hd_doctor.phone,gt.label,start_time,hd_doctor.skill,employer,gt.name as group_name')->join("left join __GROUP_TABLE__ gt on gt.id=__DOCTOR__.group_id")->where(['hd_doctor.id'=>$id])->find();
        //获取当前医生的图片信息
        $imgs = M('DoctorImg')->where(['doc_id'=>$doctor['id']])->select();
        //图片分类
        $doctor_img = [];
        foreach ($imgs as $img){
            switch ($img['flag']){
                case '1'://身份证正面
                    $doctor_img['img_identZ'] = $img['url'];
                    break;
                case '2'://身份证反面
                    $doctor_img['img_identF'] = $img['url'];
                    break;
                case '3'://学历认证
                    $doctor_img['img_edu'] = $img['url'];
                    break;
                case '4'://技能证书
                    $doctor_img['img_skills'][] = $img['url'];
                    break;
                case '5'://在职离职证明
                    $doctor_img['img_job'] = $img['url'];
                    break;
            }
        }
        //合并数据传入模板
        $doctor_info = array_merge($doctor,$doctor_img);
        $this->assign('doctor',$doctor_info);
        if(isset($flag)){
            $this->display('Worker/checked_detail');
        }else{
            $this->display('checked_detail');
        }

    }

    /**
     * ajax验证是否通过
     * @param $flag
     * @param $id
     */
    public function checkedDoctorPass($flag,$id){
        switch ($flag){
            case '0'://不通过
                $user_id = M('Doctor')->where(['id'=>$id])->getField('user_id');
                //删除doctor表和doctor_img的数据
                if(M('Doctor')->where(['id'=>$id])->delete()){
                    $res = ['status'=>1];
                }else{
                    $res = ['status'=>0];
                }
                //删除图片表
                M('doctor_img')->where(['doc_id'=>$id])->delete();
                //添加到验证失败doctor_fail表中
                M('doctor_fail')->add(['user_id'=>$user_id,'time'=>time()]);
                break;
            case '1'://通过  将doctor状态改为1
                $doctor_model = M('Doctor');
                if($doctor_model->where(['id'=>$id])->setField(['status'=>1])){
                    $res = ['status'=>1];
                }else{
                    $res = ['status'=>0];
                };
                //如果当前医生加入的小组 将医生加入到小组
                $doctor_info = $doctor_model->where(['id'=>$id])->field('group_id,user_id')->find();
                M('group_member')->add(['groupid'=>$doctor_info['group_id'],'userid'=>$doctor_info['user_id']]);
                break;
        }
        $this->ajaxReturn($res);
    }

}